<?php

class Core_Model_DbTable_TrackingInput extends Zend_Db_Table_Abstract {

    protected $_name = 'tracking_input';
    
    public function getTrackingRow($user_id) {
        $expired_time = time() - 60 * 5;
        $select = $this->select()
                       ->from($this->_name)
                       ->where('status = 0')
                       ->where("created_time < $expired_time");
        $row = $this->fetchRow($select);
        if ( empty($row) ) {
            $tracking_data = array(
                'input_user' => $user_id,
                'created_time' => time()
            );
            return $this->insert($tracking_data);
        } else {
            $data = array (
                'input_user' => $user_id,
                'created_time' => time()
            );
            $this->update($data, "id = {$row['id']}");
            return $row['id'];
        }
    }
    
    public function setUsedTrackingInput($tracking_number) {
        $this->update(array("status" => 1), "id = $tracking_number");
    }
}

?>
